package com.jzdjt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.jzdjt.entity.VideoEntity;
import com.jzdjt.tool.DBUtil; 

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetCourseListServlet extends HttpServlet {

    public GetCourseListServlet() {
        super();
    }

    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	 response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
         boolean b=false;
         //根据标示名获取JSP文件中表单所包含的参数
         String stuid=request.getParameter("stuid");
         String subject=request.getParameter("subject");
          
         String grade=request.getParameter("grade");
         
         String  result = getVideoList(stuid,subject,grade);  
         
         
         PrintWriter out = response.getWriter();//回应请求
         
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result); 
    }

    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	// doGet(request, response); 
         //设置客户端的解码方式为utf-8
         response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
        
         //根据标示名获取JSP文件中表单所包含的参数
         String stuid=request.getParameter("stuid");
         String subject=request.getParameter("subject");
          
         String grade=request.getParameter("grade");
         
         String  result = getVideoList(stuid,subject,grade);  
         
         
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result); 
    }

	private String getVideoList(String stuid, String subject, String grade) {
		try {
			JSONObject jsonObject = new JSONObject();
			String sql = "";
			if(subject==null||subject.trim().equals("")){
	            return "";
	        }
	        else if(subject.contains("&")){
	            String[] subjectarry= subject.split("&");
	            String subjectsql = "";
	            subjectsql = " ( v.VideoXK = '"+subjectarry[0]+"'";
	            for(int i=1;i<subjectarry.length;i++){
	                subjectsql += " OR v.VideoXK = '"+subjectarry[i]+"'";
	            }
	            subjectsql += ") ";
	            sql = "select v.* from StudyVideo v,paperwrongrecord p where  p.paperid = v.paperid and v.Grade='"+grade+"' and  "+subjectsql+" and v.videoStatus='已发布' and p.questionNum = v.question  and p.studentid ='"+stuid+"' order by videoTime desc ";
	            
	        }
	        else{
	        	sql = "select v.* from StudyVideo v,paperwrongrecord p where  p.paperid = v.paperid and v.Grade='"+grade+"' and v.VideoXK='"+subject+"' and  v.videoStatus='已发布' and p.questionNum = v.question  and p.studentid ='"+stuid+"' order by videoTime desc ";
	        }  
	        DBUtil dbUtil = new DBUtil();
	        List<VideoEntity> videolist = dbUtil.QueryFromVideo(sql);
	        JSONArray array =  JSONArray.fromObject(videolist);
	        return array.toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "";
		}
		
	}

}